var queues = require('mysql-queues');
const DEBUG = true;
var select = function(connection, callback) {
	var sql = 'select * from performance_term';
	connection.query(sql, function(err, rows) {
		if (err){
			console.log(err);
			console.log(err.stack);
		}
		callback(rows);
		connection.release();
	});
},
	add = function(connection, data, callback) {
		queues(connection, DEBUG);
		var sql = 'insert into performance_term(term,content,weight,completion,score,type,performanceId) values(?,?,?,?,?,?,?)';
		var trans = connection.startTransaction(); 
		trans.query(sql, [data.term,data.content,data.weight,data.completion,data.score,data.type,data.performanceId], function(err, rows) {
			if (err) {
				console.log(err);
				console.log(err.stack);
				trans.rollback();
			} else {
				trans.commit(function(err, info) {
					console.log(info);
				});
			}
			callback(rows);
		});
		trans.execute();
		console.log('execute addUserRole');
		connection.release();
	},
	delet = function(connection, id, callback) {
		queues(connection, DEBUG);
		var sql = 'delete from performance_term where id=?';
		var trans = connection.startTransaction();
		trans.query(sql, id, function(err, rows) {
			if (err) {
				console.log(err);
				console.log(err.stack);
				trans.rollback();
			} else {
				trans.commit(function(err, info) {
					console.log(info);
				});
			}
			console.log('rows', rows);
			callback(rows);
		});
		trans.execute();
		console.log('execute');
		connection.release();
	},
	update = function(connection, data, callback) {
		queues(connection, DEBUG);
		var sql = 'update performance_term set term=?,content=?,weight=?,completion=?,score=?,type=?,performanceId=? where id=?';
		var trans = connection.startTransaction();
		trans.query(sql, [data.term,data.content,data.weight,data.completion,data.score,data.type,data.performanceId,data.id], function(err, rows) {
			if (err) {
				console.log(err);
				console.log(err.stack);
				trans.rollback();
			} else {
				trans.commit(function(err, info) {
					console.log('info:', info);
				});
			}
			console.log('rows', rows);
			callback(rows);
		});
		trans.execute();
		console.log('execute update');
		connection.release();
	},
	//根据绩效id查询绩效项信息
	selTermByPid = function(connection, pid, callback) {
		var sql = "select pt.*,e.name,e.email from performance_term pt,employee e where pt.assPersonId=e.id and pt.performanceId = ?";
		// var sql = "select * from performance_term where performanceId = ?";
		connection.query(sql, pid, function(err, rows) {
			if(err) {
				console.log(err);
				console.log(err.stack);
				callback(null);
			}else{
				callback(rows);
			}
			connection.release();
		});
	},
	//根据绩效id查询状态为1的绩效项信息
	selCheckedTermByPid = function(connection, pid, callback) {
		// console.log("selCheckedTermByPid");
		var sql = "select * from performance_term where performanceId = ? and state = 1";
		connection.query(sql, pid, function(err, rows) {
			if(err) {
				console.log(err);
				console.log(err.stack);
				callback(null);
			}else{
				callback(rows);
			}
			connection.release();
		});
	},

	//根据绩效id查询所有未审核的绩效项信息
	selUncheckTermByPid = function(connection, pid, callback) {
		// console.log("selUncheckTermByPid");
		var sql = "select * from performance_term where performanceId = ? and state = 0";
		connection.query(sql, pid, function(err, rows) {
			if(err) {
				console.log(err);
				console.log(err.stack);
				callback(null);
			}else{
				callback(rows);
			}
		});
	},

	//根据绩效id及审核人id查询绩效项信息
	selTermByPidAssm = function(connection, pid, assmId, callback) {
		// var sql = "select * from performance_term where performanceId = ? and assPersonId = ?";
		var sql = "select p.*,e.name from performance_term p,employee e where p.assPersonId=e.id and p.performanceId = ? and p.assPersonId = ?";
		connection.query(sql, [pid, assmId], function(err, rows) {
			if(err) {
				console.log(err);
				console.log(err.stack);
				callback(null);
			}else{
				callback(rows);
			}
			connection.release();
		});
	},
	findById = function(connection, id, callback) {
		var sql = 'select * from performance_term where id=?';
		connection.query(sql, id, function(err, rows) {
			callback(rows);
			connection.release();
		});
	},
	selectPerTerm = function(connection, id, callback){
		var sql = 'select p.id,p.term,p.content,p.weight,p.completion,p.score,p.state,p.type,p.assPersonId,e.name from performance_term p,employee e where p.performanceId=? and p.assPersonId=e.id';
		connection.query(sql, id, function(err, rows){
			callback(rows);
			connection.release();
		});
	};

module.exports = {
	selectPerTerm: select,
	addPerTerm: add,
	deletePerTerm: delet,
	updatePerTerm: update,
	findById: findById,
	selTermByPid: selTermByPid,
	selTermByPidAssm: selTermByPidAssm,
	selUncheckTermByPid: selUncheckTermByPid,
	selCheckedTermByPid: selCheckedTermByPid,
	selectPerTerms:selectPerTerm
};